package com.cx.common.utils;

import org.apache.commons.lang.ArrayUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @Author:cxy
 * @Date:2019/10/24 15:11
 */
public class StringUtils extends org.apache.commons.lang.StringUtils {


	public static String[] starArr = {"*","***"};

	public static String defaultIfBlank(String str, String defaultStr) {
		return StringUtils.isBlank(str) ? defaultStr : str;
	}

	public static String format(String info, Object... objects) {
		if (ArrayUtils.isEmpty(objects)) {
			return info;
		}
		int length = objects.length;
		try {
			for (int i = 0; i < length; i++) {
				Object object = objects[i];
				Class<?> clazz = object.getClass();
				if (clazz.isAssignableFrom(int.class) || clazz.isAssignableFrom(Integer.class)) {
					info = replaceFromIndex(info, "{}", "%d", i);
				}
				if (clazz.isAssignableFrom(double.class) || clazz.isAssignableFrom(Double.class)) {
					info = replaceFromIndex(info, "{}", "%f", i);
				}
				if (clazz.isAssignableFrom(String.class)) {
					info = replaceFromIndex(info, "{}", "%s", i);
				}
				if (clazz.isAssignableFrom(boolean.class) || clazz.isAssignableFrom(Boolean.class)) {
					info = replaceFromIndex(info, "{}", "%b", i);
				}
			}
			String format = String.format(info, objects);
			return format;
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return StringUtils.EMPTY;
	}

	/**
	 * 从第几个位置替换那一个
	 *
	 * @param str
	 *            源字符串
	 * @param target
	 *            需要替换的目标子串
	 * @param replacement
	 *            需要替换成的字符串
	 * @param targetIndex
	 *            替换第几个
	 * @return
	 * @throws NullPointerException
	 * @throws Exception
	 */
	public static String replaceFromIndex(String str, String target, String replacement, int targetIndex)
			throws NullPointerException, IllegalArgumentException {
		if (str == null || target == null || replacement == null) {
			throw new NullPointerException();
		}
		int index = str.indexOf(target, targetIndex);
		if (index == -1) {
			throw new IllegalArgumentException(str + " Not Found " + target);
		}
		String str1 = str.substring(0, index);
		String str2 = str.substring(index);
		str2 = org.apache.commons.lang.StringUtils.replace(str2, target, replacement, 1);
		return str1 + str2;
	}

	/**
	 * 字符串去重方法一
	 * 
	 * @param str
	 * @return
	 */
	public static String ReDuplication(String str) {
		StringBuffer result = new StringBuffer();
		List<Character> list = new ArrayList<Character>();
		char[] cs = str.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			if (!list.contains(cs[i])) {
				result.append(cs[i]);
				list.add(cs[i]);
			}
		}
		return result.toString();
	}

	/**
	 * 字符串去重方法二
	 * 
	 * @param str
	 * @return
	 */
	static String ReDuplicationTwo(String str) {
		List<Character> list = new ArrayList<Character>();
		StringBuffer sb = new StringBuffer(str);
		int j = 0;
		for (int i = 0; i < str.length(); i++) {
			if (list.contains(str.charAt(i))) {
				sb.deleteCharAt(i - j); // String 是没有delete方法的
				j++; // 因为删除了sb中的字符，有一个偏移
			} else {
				list.add(str.charAt(i));
			}
		}
		return sb.toString();
	}

	/***
	 * 是否中文
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChineseChar(String str) {
		boolean temp = false;
//		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Pattern p = Pattern.compile("[a-zA-Z]");
		Matcher m = p.matcher(str);
		if (!m.find()) {
			temp = true;
		}
		return temp;
	}

	/**
	 * 返回长度为【strLength】的随机数，在前面补0
	 * 
	 * @param strLength
	 * @return
	 */
	public static String getFixLenthString(int strLength) {

		Random rm = new Random();

		// 获得随机数
		double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

		// 将获得的获得随机数转化为字符串
		String fixLenthString = String.valueOf(pross);

		// 返回固定的长度的随机数
		return fixLenthString.substring(1, strLength + 1);
	}

	/****
	 * 判断是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumericPattern(String str) {
		Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public static String getStarChar(String str) {
		int length = str.length();
		if (length <= 1) {
			return str;
		}
		String replaceStr = EMPTY;
		String substring = EMPTY;
		if (length <= 2) {
			substring = substring(str, 1, length);
			replaceStr = starArr[0];
			return replaceFromIndex(str, substring, replaceStr, 1);
		}
		substring = substring(str, 1, length - 1);
		replaceStr = starArr[1];
		return replaceFromIndex(str, substring, replaceStr, 1);
	}
	
	public static long ipToInt(String strIp) {
		try {
            long[] ip = new long[4];
            String[] ipArr = strIp.split("\\.");
            // 将每个.之间的字符串转换成整型
            for (int i = 0; i < ipArr.length; i++) {
                ip[i] = Long.parseLong(ipArr[i]);
            }
            return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
        }catch (Exception e){
            return 0L;
        }
	}
}
